package com.itheima.task;

import com.itheima.common.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

import java.time.LocalDateTime;
import java.util.Set;

/**
 * 定时处理垃圾图片
 */
@Component
@Slf4j
public class OrderTask {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private AliOssUtil aliOssUtil;

    /**
     * 定时比较redis中2个set集合
     */
    @Scheduled(cron = "0 0 0,12 * * ?")
    public void executeTask () {
        log.info("定时比较redis中2个set集合:{}", LocalDateTime.now());
        Jedis jedis = new Jedis();
        Set<String> difference = jedis.sdiff("picture", "newPicture");
        for (String s : difference) {
            aliOssUtil.delete(s);
        }
        jedis.close();
    }
}
